package com.ssh.controller.login;

import com.common.service.CommonService;
import com.ssh.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 文 件 名 称：LoginController.
 * 文 件 说 明：
 * 创   建   人：zpen.
 * 创 建 时 间：2018/3/5 17:09.
 */
@Controller
@RequestMapping(value = "/login.action")
public class LoginController {

    @Autowired
    CommonService commonService;

    @RequestMapping
    public String login(){
        return "login/login";
    }


    @RequestMapping(params = "method=loginVerify")
    @ResponseBody
    public Map<String,Object> loginVerify(HttpServletRequest request, String username, String password){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = (User) commonService.getEntityByProperty(User.class,"username", username);
            if (user == null) {
                map.put("msg", "null");
            } else {
                if (password.equals(user.getPassword()) && user.getState()==1) {
                    request.getSession() .setAttribute("user", user);
                    map.put("msg", "success");
                    return map;
                } else if (user.getState()==0) {
                    map.put("msg", "delete");
                } else if (!password.equals(user.getPassword())) {
                    map.put("msg", "errorPass");
                } else {
                    map.put("msg", "fail");
                }
            }
        }catch (Exception e){
            map.put("msg", "fail");
            System.out.println(e.toString());
        }
        return map;
    }

}
